/*
 * Copyright 2002-2006 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package reportcenter.support;


import org.springframework.core.closure.Constraint;
import org.springframework.rules.Rules;
import org.springframework.rules.support.DefaultRulesSource;
import reportcenter.auth.dominio.Roles;
import reportcenter.auth.dominio.Users;
import reportcenter.datasource.dominio.DataSource;
import reportcenter.report.dominio.report.Report;

/**
 * This class is a source for validation rules associated with the domain objects in this application. This clas is
 * wired into application via the application context configuration like this:
 * 
 * <pre>
 *    &lt;bean id=&quot;rulesSource&quot; 
 *        class=&quot;org.springframework.richclient.samples.simple.domain.SimpleValidationRulesSource&quot;/&gt;
 * </pre>
 * 
 * With this configuration, validating forms will interrogate the rules source for rules that apply to the class of a
 * form object (in this case, that's objects of type {@link Contact}.
 * @author Larry Streepy
 */
public class ValidadorBase extends DefaultRulesSource {

    /**
     * Basic name validator. Note that the "alphabeticConstraint" argument is a message key used to locate the message
     * to display when this validator fails.
     */
    private final Constraint NAME_CONSTRAINT = all(new Constraint[]{required(), minLength(2),
                regexp("[-'.a-zA-Z ]*", "alphabeticConstraint")
            });
    /** Zipcode validator, allows NNNNN or NNNNN-NNN */
    private final Constraint ZIPCODE_CONSTRAINT = all(new Constraint[]{required(), minLength(5), maxLength(10),
                regexp("[0-9]{5}(-[0-9]{3})?", "zipcodeConstraint")
            });
    /** Email validator, simply tests for x@y, wrap in ()? so it is optional */
    private final Constraint EMAIL_CONSTRAINT = all(new Constraint[]{regexp("([-a-zA-Z0-9.]+@[-a-zA-Z0-9.]+)?",
                "emailConstraint")
            });
    /** Phone number validator, must be 55-3456-7890, wrap in ()? so it is optional */
    private final Constraint PHONE_CONSTRAINT = all(new Constraint[]{regexp("([0-9]{2}-[0-9]{4}-[0-9]{4})?",
                "phoneConstraint")
            });

    /**
     * Construct the rules source. Just add all the rules for each class that will be validated.
     */
    public ValidadorBase() {
        super();

        // Add the rules specific to the object types we manage
        addRules(createAdminReportRules());
        addRules(createDataSourceRules());
        addRules(createUserAuthRules());
        addRules(createRolesRules());
    }

    /**
     * Construct the rules that are used to validate a Contact domain object.
     * @return validation rules
     * @see Rules
     */
    private Rules createAdminReportRules() {
        // Construct a Rules object that contains all the constraints we need to apply
        // to our domain object. The Rules class offers a lot of convenience methods
        // for creating constraints on named properties.

        return new Rules(Report.class) {

            protected void initRules() {
                add("title", required());
                add("subtitle", required());
                add(not(eqProperty("title", "subtitle")));
                add("query", required());
                add("idD", required());
                add("idRole", required());
            }
        };
    }
    
    private Rules createDataSourceRules() {           
        return new Rules(DataSource.class) {

            protected void initRules() {
                add("name", required());
                add("driver", required());
                add("uri", required());
                add("usu", required());
                add("pass", required());
                add("minpool", required());
                add("maxpool", required());
            }
        };
    }
    
    private Rules createUserAuthRules() {           
        return new Rules(Users.class) {

            protected void initRules() {
                add("password", new Constraint[]{required(), minLength(4),maxLength(10),regexp("[-'.a-zA-Z0-9.]*", "passwordConstraint")});
                add("username", new Constraint[]{required(), minLength(4),maxLength(10),regexp("[-'.a-zA-Z0-9.]*", "usernameConstraint")});
                
            }
        };
    }

    private Rules createRolesRules(){
        return new Rules(Roles.class) {
            
            protected void initRules(){
                add("roleName", new Constraint[]{required(), minLength(6), maxLength(30),regexp("[_'.A-Z ]*", "roleNameConstraint")});
            }
            
        };
    }
}
